#include "curve.h"
#include "ui_curve.h"
#include <QDebug>
#include <QPointF>
#include <QPainter>
#include <QPainterPath>

Curve::Curve(QByteArray data, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Curve), dat(data)
{
    ui->setupUi(this);
    initialize();
}

Curve::~Curve()
{
    delete ui;
}


void Curve::initialize()
{
}

void Curve::paintEvent(QPaintEvent *)
{
    qint16* pdata = (qint16*)dat.data();
//纵轴表示-200V至500V的电压,dy为第1V对应的y轴长度
    float dy = ((float)this->height()-10) / 700;
//    qDebug()<<"dy:"<<dy;
//x轴表示20ms,dx为第0.1ms对应的x轴长度
    float dx = ((float)this->width()-10) / 200;
//    qDebug()<<"dx:"<<dx;

//算出200个点的位置
    QPointF points[200];

    for(int i = 0; i < 200; i++) {
        points[i] = QPointF(i * dx+10, (500 - (* (pdata + i)*0.1)) * dy);
    }

    //画刻度盘
    QPainterPath path;
    path.moveTo(10,0);
    path.lineTo(10, this->height());
    for(int i=0;i<700;i+=100){
        path.moveTo(10,dy*i);
        path.lineTo(0,dy*i);
//        path.addText(10,dy*i,QFont(),QString::number(500-i)+"V");
    }
    for(int i=0;i<700;i+=10){
        path.moveTo(10,dy*i);
        path.lineTo(5,dy*i);
    }
    path.moveTo(0,this->height()-10);
    path.lineTo(this->width(),this->height()-10);
    for(int i=0;i<200;i+=100){
        path.moveTo(dx*i+10,this->height()-10);
        path.lineTo(dx*i+10,this->height());
    }
    for(int i=0;i<200;i+=10){
        path.moveTo(dx*i+10,this->height()-10);
        path.lineTo(dx*i+10,this->height()-5);
    }


    QPainter painter(this);
    painter.drawPath(path);
    painter.setPen(Qt::darkYellow);
    painter.drawText(15,10,"500V");
    painter.drawText(15,dy*100,"400V");
    painter.drawText(15,dy*200,"300V");
    painter.drawText(15,dy*300,"200V");
    painter.drawText(15,dy*400,"100V");
    painter.drawText(15,dy*500,"0V");
    painter.drawText(15,dy*600,"-100V");
    painter.drawText(15,dy*700,"-200V");
    painter.drawText(dx*100,this->height()-10,"10ms");
    painter.drawText(this->width()-25,this->height()-10,"20ms");

    painter.setPen(Qt::blue);
    painter.drawLine(10,dy*500,this->width(),dy*500);

    //画曲线
    QPainterPath curvepath;
    curvepath.moveTo(points[0]);
    for(int i=0;i<200;i++){
        curvepath.lineTo(points[i]);
    }
    painter.setPen(QPen(Qt::green,2));
    painter.drawPath(curvepath);

}
